package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.NotifyDao;
import com.youmu.sichuansfc.domain.YoumuNotify;
import com.youmu.sichuansfc.domain.pojo.NotifyReadPojo;
import com.youmu.sichuansfc.dto.PageT;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WYZ
 * @create 2018-01-31 18:37
 * @desc
 **/
@Repository
public class NotifyDaoImpl  implements NotifyDao{

    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public YoumuNotify upload(YoumuNotify youmuNotify) {

        if(youmuNotify.getId() != null) {
            em.merge(youmuNotify);
        }else {
            em.persist(youmuNotify);
        }
        return youmuNotify;
    }

    @Override
    public PageT findBy(Date startTime, Date endTime, String title , int num, int size, int type) {
        StringBuffer sql = new StringBuffer();
        sql.append("from  YoumuNotify where is_delete = 0 and type =:type \n");

        Map<String, Object> map = new HashMap<>();
        map.put("type", type);

        if (startTime != null){
            sql.append(" and add_time >=:startTime \n");
            map.put("startTime", new Timestamp(startTime.getTime()));
        }
        if (endTime != null){
            sql.append(" and add_time <=:endTime \n");
            map.put("endTime", new Timestamp(endTime.getTime()));
        }
        if (StringUtils.isNotEmpty(title)){
            sql.append(" and title like :title \n");
            map.put("title", "%"+title+"%");
        }
        sql.append(" order by add_time desc \n");
        Query query = em.createQuery(sql.toString(), YoumuNotify.class);
        for(String key : map.keySet()){
            query.setParameter(key,map.get(key));
        }

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());

        List<YoumuNotify> youmuNotifies = query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList();
        pageT.setPageList(youmuNotifies);
        return pageT;
    }

    @Override
    public PageT findBy(Boolean isRead, String title, long fundUserId, int num, int size, int type) {
        Map<String, Object> param = new HashMap<>();

        StringBuffer sql = new StringBuffer();
        sql.append("select yn.*, \n");
        sql.append("((select COUNT(*) from youmu_notify_read_history ynrh where ynrh.notify_id = yn.id and ynrh.is_delete = 0 and ynrh.fund_user_id =:fundUserId) > 0 ) as is_read \n");
        sql.append("from youmu_notify yn \n");
        sql.append(" where yn.is_delete = 0  and yn.type =:type  \n");

        param.put("type", type);
        param.put("fundUserId", fundUserId);
        if(isRead != null){
            sql.append("and (select count(*) from youmu_notify_read_history ynrh where ynrh.notify_id = yn.id and ynrh.is_delete = 0 and ynrh.fund_user_id =:fundUserId2 limit 0, 1) =:isRead \n");
            param.put("fundUserId2", fundUserId);
            param.put("isRead", isRead ?  1 :  0);
        }
        if (StringUtils.isNotEmpty(title)){
            sql.append(" and yn.title like:title \n");
            param.put("title", "%"+title+"%");
        }
        sql.append(" ORDER BY yn.add_time desc \n");

        Query query = em.createNativeQuery(sql.toString(), NotifyReadPojo.class);
        for(String key : param.keySet()){
            query.setParameter(key,param.get(key));
        }

        PageT pageT = new PageT();
        List<NotifyReadPojo> notifyReadPojos = query.getResultList();
        if (notifyReadPojos != null && notifyReadPojos.size() > 0){
            pageT.setCount(notifyReadPojos.size());
        }

        notifyReadPojos = query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList();
        pageT.setPageList(notifyReadPojos);

        return pageT;
    }

    @Override
    public YoumuNotify findById(Long id) {
        String sql = "from  YoumuNotify where is_delete = 0 and id =:id";
        List<YoumuNotify> youmuNotifies = em.createQuery(sql, YoumuNotify.class)
                .setParameter("id", id)
                .getResultList();
        if (youmuNotifies != null && youmuNotifies.size() > 0 ){
            return youmuNotifies.get(0);
        }
        return null;
    }

    @Override
    public PageT findByFilter(int num, int size, int type) {
        StringBuffer sql = new StringBuffer();
        sql.append("from  YoumuNotify where is_delete = 0 and type =:type \n");

        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        sql.append(" order by read_Num desc, add_time desc  limit 0 , 10\n");
        Query query = em.createQuery(sql.toString(), YoumuNotify.class);
        for(String key : map.keySet()){
            query.setParameter(key,map.get(key));
        }

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());

        List<YoumuNotify> youmuNotifies = query.setFirstResult(num * size)
                .setMaxResults(size)
                .getResultList();
        pageT.setPageList(youmuNotifies);
        return pageT;
    }
}
